package com.ciei.dpagm.service;

import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.AutoCodeSeed;
import com.ciei.dpagm.mapper.AutoCodeSeedMapper;
import com.ciei.dpagm.util.Validator;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class AutoCodeSeedService extends BaseService<AutoCodeSeedMapper,AutoCodeSeed> {

	Map<String,Integer> seedId=new HashMap<String,Integer>();
	Map<String,Integer> seedNum=new HashMap<String,Integer>();

	/**
	 * 获得 integer 型自动编码，默认获得一个
	 * @param "pIDName"2323
	 * @param "n" 要获取得自动编码个数，用于批量添加
	 * @return integer 型数字字符串
	 */
	public Integer GetNewID(String pIDName, int n)
	{
		try
		{
			if(seedId.containsKey(pIDName)) {
				if((seedNum.get(pIDName)+n)<200)
				{
					Integer newId=seedId.get(pIDName)+n;
					seedNum.put(pIDName, seedNum.get(pIDName)+n);
					seedId.put(pIDName, newId);
					return newId;
				}
			}

			AutoCodeSeed autoCodeSeed = findById(pIDName);

			if (autoCodeSeed==null)
			{
				autoCodeSeed=new AutoCodeSeed();
				autoCodeSeed.setSeedName(pIDName);
				autoCodeSeed.setSeedValue(new Integer(10000));//没有的话，从10000开始编码，以防和系统初始化的数据冲突
				save(autoCodeSeed);

				seedNum.put(pIDName, 1);
				seedId.put(pIDName, 10000);

				return 10000;
			}
			else
			{
				Integer newId=autoCodeSeed.getSeedValue() + n;
				seedNum.put(pIDName, n);
				seedId.put(pIDName, newId);

				autoCodeSeed.setSeedValue(autoCodeSeed.getSeedValue() + (n>200?n:200));
				updateById(autoCodeSeed);

				return newId;
			}
		}
		catch (RuntimeException ex)
		{
			throw ex;
		}
	}

	/**
	 * 获得 integer 型自动编码，默认获得一个
	 * @param "pIDName"
	 * @return integer 型数字字符串
	 */
	public Integer GetNewID(String pIDName)
	{
		return updateAndGetNewId(pIDName);
	}

	public Integer updateAndGetNewId(String pIDName)
	{
		return GetNewID(pIDName,1);
	}


	/**
	 * 获得文本型自动编码，默认获得一个
	 * @param pCodeName
	 * @param n 要获取得自动编码个数，用于批量添加
	 * @return 加前缀P的10位字符串
	 */
	public String GetNewCode(String pCodeName, int n)
	{
		try
		{
			String NewCode;
			NewCode = "000000000" + GetNewID(pCodeName, n).toString();
			return "E" + NewCode.substring(NewCode.length() - 9);
		}
		catch (RuntimeException re)
		{
			throw re;
		}
	}
	/**
	 * 获得一个设备文本型自动编码
	 * @param pCodeName
	 * @return 以一位项目编号为前缀的5位字符串
	 */
	public String GetNewDeviceCode(String pCodeName)
	{
		try
		{
//			if (seedId.containsKey(pCodeName))
//			{
//				if (seedNum.get(pCodeName)<1000000)
//				{
//
//				}
//			}
			AutoCodeSeed autoCodeSeed=findById(pCodeName);
			if (autoCodeSeed!=null)
			{
				String seedValueString = autoCodeSeed.getSeedValueString();
				String newSeedValueString = deviceCodeAdd(seedValueString);
				autoCodeSeed.setSeedValueString(newSeedValueString);
				updateById(autoCodeSeed);
				return newSeedValueString;
			}else {
				return "";
			}




//			String NewCode;
//			NewCode = "000000000" + GetNewID(pCodeName, n).toString();
//			return "A" + NewCode.substring(NewCode.length() - 5);
		}
		catch (RuntimeException re)
		{
			throw re;
		}
	}

	/**
	 * 令seedValueString自增1并返回
	 * @param seedValueString	待自增字符，每个字符的取值范围为0~9 A~Z a~z
	 * @return	自增后字符，每个字符的取值范围为0~9 A~Z
	 */
	public String deviceCodeAdd(String seedValueString){
		if (seedValueString == null || Validator.notOnlyDigitAndLetter(seedValueString)) {
			// 字符串格式不正确，返回空字符串
			return "";
		}
		seedValueString = seedValueString.toUpperCase();
		char[] seedCharArray = seedValueString.toCharArray();
		boolean hasCarry = true;
		for (int i = seedCharArray.length - 1; i >= 0; i--) {
			// 从后往前获取字符，自增1
			if (seedCharArray[i] == 'Z') {
				// 进位，字符置0，继续循环
				seedCharArray[i] = '0';
			} else if (seedCharArray[i] == '9') {
				// 跳过取值范围外的部分
				seedCharArray[i] = 'A';
				// 无进位时退出循环
				hasCarry = false;
				break;
			} else {
				seedCharArray[i]++;
				// 无进位时退出循环
				hasCarry = false;
				break;
			}
		}
		if (hasCarry) {
			// 进位溢出
			return seedValueString;
		} else {
			return String.valueOf(seedCharArray);
		}
	}

	public Integer GetNewSingleCode(String pIDName, int n)
	{
		try
		{


			if(seedId.containsKey(pIDName)) {

				if((seedNum.get(pIDName)+n)<200)
				{
					Integer newId=seedId.get(pIDName)+n;
					seedNum.put(pIDName, seedNum.get(pIDName)+n);
					seedId.put(pIDName, newId);
					return newId;
				}
			}

			AutoCodeSeed autoCodeSeed = findById(pIDName);
			if (autoCodeSeed==null)
			{
				autoCodeSeed=new AutoCodeSeed();
				autoCodeSeed.setSeedName(pIDName);
				autoCodeSeed.setSeedValue(new Integer(10000));//没有的话，从10000开始编码，以防和系统初始化的数据冲突
				save(autoCodeSeed);
				seedNum.put(pIDName, 1);
				seedId.put(pIDName, 10000);

				return 10000;
			}
			else
			{
				Integer newId=autoCodeSeed.getSeedValue() + n;
				seedNum.put(pIDName, n);
				seedId.put(pIDName, newId);

				autoCodeSeed.setSeedValue(autoCodeSeed.getSeedValue() + (n>200?n:200));
				updateById(autoCodeSeed);

				return newId;
			}
		}
		catch (RuntimeException ex)
		{
			throw ex;
		}
	}

	/**
	 * 将由同种子生成的整型主键转化为字符串主键
	 * @param Id
	 * @param
	 * @return
	 */

	public String IdToCode(Integer Id){
		String NewCode;
		NewCode = "000000000" + Id.toString();
		return "E" + NewCode.substring(NewCode.length() - 9);
	}


	/**
	 * 获得文本型自动编码，默认获得一个
	 * @param pCodeName
	 * @return 加前缀P的10位字符串
	 */
	public String GetNewCode(String pCodeName)
	{
		return GetNewCode(pCodeName,1);
	}

	/**
	 * 获取最小长度的自动编码
	 * @param pCodeName
	 * @param n 长度，例如n=3，ID=1，则返回001
	 * @return
	 */
	public String GetNewStrCode(String pCodeName, int n)
	{
		try
		{
			String code = GetNewID(pCodeName).toString();
			for (int i = 0; i < n - code.length(); i++)
			{
				code = "0" + code;
			}
			return code;
		}
		catch (RuntimeException re)
		{
			throw re;
		}
	}

	/**
	 * 将由同种子生成的字符串主键转化为整型的主键
	 * @param pCodeName
	 * @param
	 * @return
	 */
	public Integer convertCodeToId(String pCodeName){
		if(pCodeName!=null&&!pCodeName.equals("")){
			int index = getCodeIndex(pCodeName);
			pCodeName = pCodeName.substring(index, pCodeName.length());
			Integer pId = null;
			try {
				pId = Integer.parseInt(pCodeName);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				return null;
			}
			return pId;
		}else{
			return null;
		}
	}


	/**
	 * 获取第一个不为0的字符位置
	 * @param pCodeName
	 * @param
	 * @return index
	 */
	public Integer getCodeIndex(String pCodeName){
		if(pCodeName!=null&&!pCodeName.equals("")){
			char[] code = pCodeName.toCharArray();
			for(int i = 1;i<code.length;i++){
				if(code[i]!='0'){
					return i;
				}
			}
			return null;
		}else{
			return null;
		}
	}


	public Object getBookingCode() {
		return GetNewStrCode("BookingCode",5);
	}
}
